package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import model.User;

/**
 * This class is a separate frame all on its own. Simple. Quick. Effective.
 * 
 * @author Tristan D. Boucher
 * @version Commit #22 9/14/2013
 */
@SuppressWarnings("serial")
public class GUIPromotionWindow extends AbstractView
{
  /**
   * The constant for the number of columns for this gui.
   */
  private static final int NUM_COLUMNS = 4;
  
  /**
   * The frame for this view.
   */
  private final JFrame my_frame;
  
  /**
   * The handle on all the UserBoxes that have been added to the gui.
   */
  private final List<UserBox> my_boxes;

  /**
   * Constructor.
   * 
   * @param the_users The users that will populate this gui.
   * @param the_controller The controller object required by AbstractView.
   */
  public GUIPromotionWindow(final List<User> the_users,
                            final GUIController the_controller)
  {
    super(the_controller, 1, 1);
    my_boxes = new ArrayList<UserBox>();
    my_frame = new JFrame();
    
    my_frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    my_frame.setLayout(new BorderLayout());
    
    final JPanel temp = new JPanel();
    temp.setLayout(new BorderLayout());
    temp.add(buildButtonPanel(), BorderLayout.SOUTH);
    temp.add(buildListPanel(the_users), BorderLayout.CENTER);
    temp.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2, true));
    
    my_frame.add(temp, BorderLayout.CENTER);
    my_frame.pack();
  }

  /**
   * Constructs the panel that will hold the buttons.
   * 
   * @return The constructed button panel.
   */
  private JPanel buildButtonPanel()
  {
    final JPanel button_panel = new JPanel();
    button_panel.setLayout(new GridLayout(1, 0));

    final JButton promote_button = new JButton("Add Admin Privileges");
    final JButton demote_button = new JButton("Remove Admin Privileges");

    button_panel.add(promote_button);
    button_panel.add(demote_button);
    
    addBackground(button_panel, BACKGROUND_COLOR);
    addBackground(promote_button, BACKGROUND_COLOR);
    addBackground(demote_button, BACKGROUND_COLOR);
    

    promote_button.addActionListener(new ActionListener()
    {
      public void actionPerformed(final ActionEvent the_e)
      {
        for (int i = 0; i < my_boxes.size(); i++)
        {
          if (my_boxes.get(i).isSelected())
          {
            my_controller.postPromotion(my_boxes.get(i).getUser(), true);
            my_boxes.get(i).setSelected(false);
          }
        }
        
        JOptionPane.showMessageDialog(GUIPromotionWindow.this, "Users Promoted", 
                                      "Promoting Done", 
                                      JOptionPane.INFORMATION_MESSAGE);
      }
    });

    demote_button.addActionListener(new ActionListener()
    {
      public void actionPerformed(final ActionEvent the_e)
      {
        for (int i = 0; i < my_boxes.size(); i++)
        {
          if (my_boxes.get(i).isSelected())
          {
            my_controller.postPromotion(my_boxes.get(i).getUser(), false);
            my_boxes.get(i).setSelected(false);
          }
        }
        JOptionPane.showMessageDialog(GUIPromotionWindow.this, "Users Demoted", 
                                      "Demoting Done", 
                                      JOptionPane.INFORMATION_MESSAGE);
      }
    });
    
    button_panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2, true));
    
    return button_panel;
  }

  /**
   * Constructs the panel that will hold the List of checkbox cells.
   * 
   * @param the_users The list of users used to make this list panel.
   * 
   * @return list_panel The completly constructed list panel with
   * all the users.
   */
  private JPanel buildListPanel(final List<User> the_users)
  {
    final JPanel list_panel = new JPanel();
    list_panel.setBackground(BACKGROUND_COLOR);

    list_panel.setLayout(new GridLayout(0, NUM_COLUMNS));
    for (int i = 0; i < the_users.size(); i++)
    {
      my_boxes.add(new UserBox(the_users.get(i)));
      my_boxes.get(i).setBackground(BACKGROUND_COLOR);
      list_panel.add(my_boxes.get(i));
    }

    list_panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2, true));
    
    return list_panel;
  }
  
  /**
   * Accessor for the frame.
   * 
   * @return my_frame The frame holding this view.
   */
  public JFrame getFrame()
  {
    return my_frame;
  }
  
  @Override
  public void setVisible(final boolean the_choice)
  {
    my_frame.setVisible(the_choice);
  }
}
